package com.example.rocketmq.base.provider;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * 发送异步消息，异步消息常常用在对响应时间敏感的业务场景，程序不能长时间等待broker相应
 */
public class AsyncProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("test_group");
        producer.setNamesrvAddr("192.168.228.101:9876");
        producer.start();
        Message msg = new Message("base", "tag2", "hello world".getBytes());

        producer.send(msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                // 发送成功回调函数
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                // 发送失败回调函数
                throwable.printStackTrace();
            }
        });

        
    }
}
